package com.example.dormitory_system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormitory_system.domain.dto.tsysuser.AnnouncementAddDTO;
import com.example.dormitory_system.domain.enetiy.Announcement;
import com.example.dormitory_system.domain.vo.student.AnnouncementDetailVO;
import com.example.dormitory_system.domain.vo.student.StuAnnouncementListVO;
import com.example.dormitory_system.domain.vo.tsysuser.AnnouncementListVO;
import com.example.dormitory_system.enums.ResultCode;
import com.example.dormitory_system.exception.ServiceException;
import com.example.dormitory_system.service.AnnouncementService;
import com.example.dormitory_system.mapper.AnnouncementMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author keenan
* @description 针对表【tb_announcement(公告表)】的数据库操作Service实现
* @createDate 2025-03-26 15:26:50
*/
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement>
    implements AnnouncementService {
    @Autowired
    private AnnouncementMapper announcementMapper;

    @Override
    public List<AnnouncementListVO> sysListAnnouncement(Long userId, String title,Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if (userId == null) {
            return announcementMapper.selectList(new LambdaQueryWrapper<Announcement>().eq(Announcement::getStatus,status)
                            .like(Announcement::getTitle,title)).
                    stream().map(item -> {
                AnnouncementListVO announcementListVO = new AnnouncementListVO();
                BeanUtil.copyProperties(item, announcementListVO);
                return announcementListVO;
            }).collect(Collectors.toList());
        }
        return announcementMapper.selectList(new LambdaQueryWrapper<Announcement>()
                .eq(Announcement::getUserId,userId)
                .eq(Announcement::getStatus,status).like(Announcement::getTitle,title)).stream().map(item -> {
                    AnnouncementListVO announcementListVO = new AnnouncementListVO();
                    BeanUtil.copyProperties(item, announcementListVO);
                    return announcementListVO;
        }).collect(Collectors.toList());
    }

    @Override
    public int addAnnouncement(AnnouncementAddDTO announcementAddDTO) {
        return announcementMapper.insert(BeanUtil.copyProperties(announcementAddDTO, Announcement.class));
    }

    @Override
    public int updateAnnouncement(Long announcementId, Integer status, Long userId) {
        if (userId == null)
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        return announcementMapper.update(new LambdaUpdateWrapper<Announcement>()
                .set(Announcement::getStatus, status)
                .set(Announcement::getUserId, userId)
                .eq(Announcement::getAnnouncementId, announcementId));
    }

    @Override
    public List<StuAnnouncementListVO> listAnnouncement(Integer pageNum, Integer pageSize, String title) {
        if (!StrUtil.isEmpty(title)) {
            return announcementMapper.selectList(new LambdaQueryWrapper<Announcement>()
                    .eq(Announcement::getStatus, 1)
                    .like(Announcement::getTitle, title)).stream()
                    .map(item -> {
                        StuAnnouncementListVO stuAnnouncementListVO = new StuAnnouncementListVO();
                        BeanUtil.copyProperties(item, stuAnnouncementListVO);
                        return stuAnnouncementListVO;
                    }).collect(Collectors.toList());
        }
        return announcementMapper.selectList(new LambdaQueryWrapper<Announcement>().eq(Announcement::getStatus,1)).stream()
                .map(item -> {
                    StuAnnouncementListVO stuAnnouncementListVO = new StuAnnouncementListVO();
                    BeanUtil.copyProperties(item, stuAnnouncementListVO);
                    return stuAnnouncementListVO;
                }).collect(Collectors.toList());
    }

    @Override
    public AnnouncementDetailVO getAnnouncementDetail(Long announcementId) {
        Announcement announcement = announcementMapper.selectOne(new LambdaQueryWrapper<Announcement>()
                .eq(Announcement::getAnnouncementId, announcementId));
        if (announcement == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        AnnouncementDetailVO announcementDetailVO = new AnnouncementDetailVO();
        BeanUtil.copyProperties(announcement, announcementDetailVO);
        return announcementDetailVO;
    }
}